package com.zl_huang.example.web;


import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ndktools.javamd5.Mademd5;
import com.zl_huang.example.model.User;
import com.zl_huang.example.model.UserRole;
import com.zl_huang.example.model.vo.UserVO;
import com.zl_huang.example.service.IUserRoleService;
import com.zl_huang.example.service.IUserService;

@Controller
public class LoginController {
	@Autowired
	private IUserService userService;
	@Autowired
	private IUserRoleService userRoleService;
	
	@RequestMapping(value = "/login" , method = RequestMethod.GET)
	public String toLoginPage(Model model){
		model.addAttribute("userVO", new UserVO());
		return "login/login";
	}
	@RequestMapping(value = "/index" , method = RequestMethod.GET)
	public String toIndex(){
		return "index/index";
	}
	@RequestMapping(value = "/logout" , method = RequestMethod.GET)
	public String doLogout(RedirectAttributes redirectAttributes , HttpSession session){
		User user = (User)session.getAttribute("user");
		if(user != null){
			session.invalidate();
			return "redirect:/login";
		}else{
			redirectAttributes.addFlashAttribute("message", "当前还未登陆，请先登陆");
			return "redirect:/index";
		}
		
	}
	@RequestMapping(value = "/login" , method = RequestMethod.POST)
	public String doLogin(RedirectAttributes redirectAttributes , UserVO vo , HttpSession session){
		if(vo != null){
			try {
				User user = userService.getUserById(vo.getUser_id());
				if(user != null){
					String codePassword = new Mademd5().toMd5(vo.getPassword());
					System.out.println("加密后的密码是这样的："+codePassword);
					Date currentTime = null;
					Calendar currentTimeCalendar = Calendar.getInstance();
					currentTimeCalendar.add(Calendar.MINUTE, -5);
					currentTime = currentTimeCalendar.getTime();
					System.out.println(currentTime+"-------------"+new Date().getTime()+"-----"+user.getLast_lock_time());
					
				if(user.getLast_lock_time().before(currentTime)){
					System.out.println("不在锁定时间范围内");
					if(codePassword.equals(user.getPassword())){
						user.setLogin_fail_count(0);
						userService.editUser(user);
						session.setAttribute("user", user);
						System.out.println(user.getUser_id());
						UserRole userRole = userRoleService.getUserRoleById(user.getUser_id());						
						if(userRole != null){
							session.setAttribute("role", userRole.getRole().getRole_id());
							System.out.println(userRole.getRole().getRole_id());
						}
						return "redirect:/index";
					}else{
						user.setLogin_fail_count(user.getLogin_fail_count() + 1);
						userService.editUser(user);						
						redirectAttributes.addFlashAttribute("message", "用户密码不正确，还可以尝试"+(3 - user.getLogin_fail_count())+"次");
						if(user.getLogin_fail_count() == 3){
							user.setLogin_fail_count(0);
							user.setLast_lock_time(new Date());
							userService.editUser(user);							
						}
						
						return "redirect:/login";
					}
				}else{
					System.out.println("目前账号被锁定，无法登陆，锁定时间范围内");
					redirectAttributes.addFlashAttribute("message", "目前账号被锁定，无法登陆，锁定时间范围内");
					return "redirect:/login";
					
				}
					
				}else{
					redirectAttributes.addFlashAttribute("message", "找不到该用户");
					return "redirect:/login";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("我进来这里了");
			
		 }else{
			redirectAttributes.addFlashAttribute("message", "出现异常，请重新登陆");
			return "redirect:/login";
		 }
		return null;
	}

}
